# -*- coding: utf-8 -*-

from django.db import models, connections
from django.conf import settings

# 基本信息模型定义

class Stock(models.Model):
    """
    个股基本信息
    """
    # 股票代码
    code = models.CharField(max_length=8, primary_key=True)
    # 股票中文简称
    short_name = models.CharField(max_length=20, db_index=True)
    # 股票拼音简称
    pinyin_name = models.CharField(max_length=20)
    # 股票中文全称
    full_name = models.CharField(max_length=100, blank=True)
    # 股票证监会一级分类
    csrc_industry = models.CharField(max_length=40)
    # 股票证监会二级分类
    csrc_sub_industry = models.CharField(max_length=20)

    def __unicode__(self):
        return self.short_name + '(' + self.code + ')'
    
    @staticmethod
    def get_stock_name_set():
        stock_name_set = set()
        for stock in Stock.objects.all():
            stock_name_set.add(stock.short_name)
        return stock_name_set

class StockAlias(models.Model):
    """
    股票别称
    """
    stock = models.ForeignKey(Stock)
    # 股票中文别称
    alias = models.CharField(max_length=20)
    created_at = models.DateTimeField('date created', auto_now_add=True)

class Industry(models.Model):
    """
    行业基本信息
    """
    # 行业名称
    name = models.CharField(max_length=40, primary_key=True)
    # 相关个股
    stocks = models.ManyToManyField(Stock, through='StockIndustryMap')

    def __unicode__(self):
        return self.name

    @staticmethod
    def get_industry_name_set():
        industry_name_set = set()
        # 将证监会行业二级分类和新浪行业分类一并加入
        for stock in Stock.objects.all():
            industry_name_set.add(stock.csrc_sub_industry)
        for industry in Industry.objects.all():
            industry_name_set.add(industry.name)
        return industry_name_set

class StockIndustryMap(models.Model):
    """
    股票行业映射关系
    """
    stock = models.ForeignKey(Stock)
    industry = models.ForeignKey(Industry)
    created_at = models.DateTimeField('date created', auto_now_add=True)
   
    class Meta:
        unique_together = ('stock', 'industry')

class Location(models.Model):
    """
    地域基本信息
    """
    # 行业名称
    name = models.CharField(max_length=40, primary_key=True)
    # 相关个股
    stocks = models.ManyToManyField(Stock, through='StockLocationMap')

    def __unicode__(self):
        return self.name

class StockLocationMap(models.Model):
    """
    股票地域映射关系
    """
    stock = models.ForeignKey(Stock)
    location = models.ForeignKey(Location)
    created_at = models.DateTimeField('date created', auto_now_add=True)

    class Meta:
        unique_together = ('stock', 'location')

class Concept(models.Model):
    """
    概念基本信息
    """
    # 概念名称
    name = models.CharField(max_length=40, primary_key=True)
    # 相关个股
    stocks = models.ManyToManyField(Stock, through='StockConceptMap')

    def __unicode__(self):
        return self.name

class StockConceptMap(models.Model):
    """
    股票概念映射关系
    """
    stock = models.ForeignKey(Stock)
    concept = models.ForeignKey(Concept)
    created_at = models.DateTimeField('date created', auto_now_add=True)

    class Meta:
        unique_together = ('stock', 'concept')

class Institute(models.Model):
    """
    券商基本信息
    """
    # 券商中文简称
    short_name = models.CharField(max_length=20, primary_key=True)
    # 券商中文全称
    full_name = models.CharField(max_length=100, blank=True)
    
    def __unicode__(self):
        return self.short_name
    
    @staticmethod
    def get_institute_name_map():
        institute_name_map = dict()
        instituion_suffix = settings.INSTITUTE_NAME_SUFFIX
        for institute in Institute.objects.all():
            institute_name = institute.short_name
            # 加入券商全称
            institute_name_map[institute_name] = institute_name
            # 去除‘证券’后缀后再加入
            pos = institute_name.find(instituion_suffix)
            if pos != -1:
                institute_name_map[institute_name[:pos]] = institute_name
        return institute_name_map

